// ============================================================================
//
// Copyright (C) 2009-2010 Gomai Inc. - www.mysoftlife.com
//
// ============================================================================
package com.sc.vessel.info.editor;

import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.ManagedForm;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.EditorPart;

import com.cs.vessel.breakdown.CDOHibernateUtil;
import com.ibm.team.ui.editor.TeamFormSectionPart;
import com.ibm.team.workitem.ide.ui.internal.editor.TeamSashForm;
import com.ibm.team.workitem.ide.ui.internal.editor.WorkItemTeamFormSectionPart;
import com.sc.vessel.info.model.breakdown.BreakdownPackage;

/**
 * DOC liaoqc class global comment. Detailled comment
 */
public class InfoEditor extends EditorPart implements IHandlerListener {

    public static String ID = "com.sc.vessel.info.editor.InfoEditor"; //$NON-NLS-1$

    private ManagedForm mform;

    private ScrolledForm sform;

    private ConfigPart configFormPart;

    private DescriptionPart phenomenonPart;

    private InfoInput infoInput;

    private DescriptionPart processPart;

    private DescriptionPart analyzePart;

    private DescriptionPart experiencePart;

    /**
     * DOC liaoqc InfoEditor constructor comment.
     */
    public InfoEditor() {
        setPartName(Messages.InfoEditor_1);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public void doSave(IProgressMonitor monitor) {
        infoInput.setChanged(false);
        // save the Accident
        try {
            CDOHibernateUtil.addOrChangeAccident(infoInput.getAccident());
        } catch (CommitException e) {
            // add failed log
            e.printStackTrace();
        }
        firePropertyChange(PROP_DIRTY);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#doSaveAs()
     */
    @Override
    public void doSaveAs() {
        // TODO Auto-generated method stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
     */
    @Override
    public void init(IEditorSite site, IEditorInput input) throws PartInitException {
        setSite(site);
        setInput(input);
        infoInput = (InfoInput) input;
        setPartName(Messages.InfoEditor_2);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#isDirty()
     */
    @Override
    public boolean isDirty() {
        return infoInput.isChanged();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
     */
    @Override
    public boolean isSaveAsAllowed() {
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createPartControl(Composite parent) {

        // Composite composite = new Composite(parent, SWT.NONE);
        // composite.setLayout(new GridLayout());
        // composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
        FormToolkit toolkit = new FormToolkit(parent.getDisplay());
        sform = toolkit.createScrolledForm(parent);
        sform.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));

        mform = new ManagedForm(toolkit, sform);

        Composite body = sform.getBody();
        body.setLayout(new GridLayout(1, false));

        TeamSashForm tab = new TeamSashForm(body, SWT.VERTICAL);
        tab.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
        mform.getToolkit().adapt(tab);

        FormData data = new FormData();
        data.left = new FormAttachment(0);
        data.right = new FormAttachment(100, 0);
        data.top = new FormAttachment(0);
        data.bottom = new FormAttachment(100, 0);

        // phenomenon
        phenomenonPart = new DescriptionPart(3, 3, 20000, BreakdownPackage.ACCIDENT__PHENOMENON);
        phenomenonPart.addHandlerListener(this);
        phenomenonPart.setInput(getEditorInput());
        TeamFormSectionPart descriptionSectionPart = new WorkItemTeamFormSectionPart(mform, tab, ExpandableComposite.TITLE_BAR
                | ExpandableComposite.EXPANDED, Messages.InfoEditor_4, new DescriptionPart[] { phenomenonPart });
        if (descriptionSectionPart != null) {

            descriptionSectionPart.getSection().setLayoutData(data);
            mform.addPart(descriptionSectionPart);
            descriptionSectionPart.setFormInput(getEditorInput());
        }

        // process
        processPart = new DescriptionPart(3, 3, 20000, BreakdownPackage.ACCIDENT__PROCESS);
        processPart.addHandlerListener(this);
        processPart.setInput(getEditorInput());
        TeamFormSectionPart processSectionPart = new WorkItemTeamFormSectionPart(mform, tab, ExpandableComposite.TITLE_BAR
                | ExpandableComposite.EXPANDED, Messages.InfoEditor_5, new DescriptionPart[] { processPart });
        if (descriptionSectionPart != null) {

            processSectionPart.getSection().setLayoutData(data);
            mform.addPart(processSectionPart);
            processSectionPart.setFormInput(getEditorInput());
        }

        analyzePart = new DescriptionPart(3, 3, 20000, BreakdownPackage.ACCIDENT__ANALYZE);
        analyzePart.addHandlerListener(this);
        analyzePart.setInput(getEditorInput());
        TeamFormSectionPart analyzeSectionPart = new WorkItemTeamFormSectionPart(mform, tab, ExpandableComposite.TITLE_BAR
                | ExpandableComposite.EXPANDED, Messages.InfoEditor_6, new DescriptionPart[] { analyzePart });
        if (analyzeSectionPart != null) {

            analyzeSectionPart.getSection().setLayoutData(data);
            mform.addPart(analyzeSectionPart);
            analyzeSectionPart.setFormInput(getEditorInput());
        }

        experiencePart = new DescriptionPart(3, 3, 20000, BreakdownPackage.ACCIDENT__EXPERIENCE);
        experiencePart.addHandlerListener(this);
        experiencePart.setInput(getEditorInput());
        TeamFormSectionPart experienceSectionPart = new WorkItemTeamFormSectionPart(mform, tab, ExpandableComposite.TITLE_BAR
                | ExpandableComposite.EXPANDED, Messages.InfoEditor_7, new DescriptionPart[] { experiencePart });
        if (experienceSectionPart != null) {

            experienceSectionPart.getSection().setLayoutData(data);
            mform.addPart(experienceSectionPart);
            experienceSectionPart.setFormInput(getEditorInput());
        }

        configFormPart = new ConfigPart();
        configFormPart.addHandlerListener(this);
        configFormPart.setInput(getEditorInput());
        TeamFormSectionPart formSectionPart = new WorkItemTeamFormSectionPart(mform, tab, ExpandableComposite.TITLE_BAR
                | ExpandableComposite.TWISTIE, Messages.InfoEditor_3, new ConfigPart[] { configFormPart });
        // sform.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        if (formSectionPart != null) {
            formSectionPart.getSection().setLayoutData(data);
            mform.addPart(formSectionPart);
            formSectionPart.setFormInput(getEditorInput());
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
     */
    @Override
    public void setFocus() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#dispose()
     */
    @Override
    public void dispose() {
        super.dispose();
        mform.dispose();
        sform.dispose();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.IHandlerListener#handlerChanged(org.eclipse.core.commands.HandlerEvent)
     */
    @Override
    public void handlerChanged(HandlerEvent handlerEvent) {
        infoInput.setChanged(true);
        firePropertyChange(InfoEditor.PROP_DIRTY);
        updateSaveButtonState();
    }

    /**
     * DOC liaoqc Comment method "updateSaveButtonState".
     */
    private void updateSaveButtonState() {
        // TODO Auto-generated method stub

    }

}
